package com.jmm.api.controller.user;

import cn.hutool.core.map.MapUtil;
import com.jmm.api.service.user.SocialService;
import com.jmm.common.annotation.Log;
import com.jmm.common.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("/api/social")
@Api(tags = { "社交网站三方登录" })
public class SocialController {
    @Autowired
    private SocialService socialService;
    @Autowired
    private WxMpService wxMpService;
    @GetMapping("code2Token/{code}")
    @Log("api:code换Token")
    @ApiOperation("api code 换取Token 或SocialToken")
    public Result<?> code2Token(@PathVariable("code")String code) {
        return socialService.code2Token(code);
    }

    @PostMapping("getUrl")
    @Log("Api:获取微信认证链接")
    @ApiOperation("传入target链接，获取微信认证链接")
    public Result<?> getUrl(@RequestParam("url")@NotNull String url){
        String targetUrl = wxMpService.oauth2buildAuthorizationUrl(url, WxConsts.OAuth2Scope.SNSAPI_BASE, "");
        return Result.ok(MapUtil.builder("redirectUrl",targetUrl).build());
    }
}
